﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using weixin.Models;

namespace weixin.Services
{
    public class AdminService
    {
        private readonly weixin.Models.WeixinOpenPlatEntities db = new Models.WeixinOpenPlatEntities();

        public tb_Admin Login(string username, string password)
        {
            tb_Admin model = db.tb_Admin.Where(r => r.loginName == username).FirstOrDefault();
            if (model != null && Tencent.Cryptography.GetMD5(password, model.loginSalt) == model.loginPwd)
            {
                model.LoginToken = Guid.NewGuid().ToString().Replace("-", "");
                model.logintimes = model.logintimes + 1;
                model.loginDate = DateTime.Now;
                db.SaveChanges();
                return model;
            }
            return null;
        }

        public tb_Admin GetByToken(string token)
        {
            tb_Admin model = db.tb_Admin.Where(r => r.LoginToken == token).FirstOrDefault();
            return model;
        }
    }
}